import json


from api.base_service import BaseService


class MemberService(BaseService):

    def __init__(self):
        super().__init__()
        self.service_name = "MemberService"

    def find_by_telephone(self, tel):
        """
        根据手机号 查询会员信息
        :param tel: 手机号 字符串类型
        :return: 查询成功，返回会员信息；如果查询失败，返回None
        """
        resp = self.dubbo_client.invoke(self.service_name, "findByTelephone", tel)
        if resp == "Failed":
            return None
        else:
            resp = json.loads(resp)
            return resp

    def add(self, member):
        """
        根据信息，添加会员
        :param member: Member 实例
        :return: 添加成功，返回True；添加失败，返回是False
        """
        member["class"] = "com.itheima.pojo.Member"
        resp = self.dubbo_client.invoke(self.service_name, "add", member)
        if resp == "null":
            return True
        else:
            return False

    def find_member_count_by_months(self, months):
        """
        根据月份，查询会员数
        :param months: 列表-列表中的元素是月份。示例 ["2020-09", "2022.10"]
        :return: 列表-列表中的元素是对应月份的会员数 示例[10, 10]
        """
        resp = self.dubbo_client.invoke(self.service_name, "findMemberCountByMonths", months)
        return json.loads(resp)


if __name__ == '__main__':
    ms = MemberService()
    member = {
        "phoneNumber": "18210362001",
        "name": "xiaoming",
        "password": "123456",
        "birthday": "1990-06-06 10:01:01",
        "regTime": "2022-04-03"
    }
    result = ms.add(member)
    print(result)
    months = ["2022-06", "2022.13"]
    result = ms.find_member_count_by_months(months)
    print(result)